#include"sqlite.hpp"
#include<cassert>

int select_stu_callback(void* arg, int col_count, char** result, char** fields_name){
    //arg:输出型参数。col_count:列数。result:存储每一行的各个字段的字符指针数组。fiels_name:每一列的字段名称（这里只有一列，所以数组里就一个元素）
    //一行调用一次
    std::vector<std::string> *arry=(std::vector<std::string>*) arg;
    arry->push_back(result[0]);//因为查出来只有一列，所以每行只有一个元素。
    
    return 0;//必须，否则abort
}
int main() { 
    SqliteHelper helper("./test.db");
    //1.创建/打开库文件
    assert(helper.open());

    //2.创建表（不存在则创建），学生信息：学号，姓名，年龄
    const char *ct = "create table if not exists student(sn int primary key, name varchar(32), age int);";//可以发现sqlite不支持auto increment自增
    assert(helper.exec(ct,nullptr,nullptr));

    //3.新增数据，修改，删除，查询
    //const char *insert_sql = "insert into student values(1, '小明', 18), (2, '小黑', 19), (3, '小红', 18);";
    //assert(helper.exec(insert_sql, nullptr, nullptr));

    //const char *update_sql = "update student set name = '张晓明' where sn = 1";
    //assert(helper.exec(update_sql, nullptr, nullptr));

    //const char *delete_sql = "delete from student where sn = 3";
    //assert(helper.exec(delete_sql, nullptr, nullptr));

    const char *select_sql = "select name from student;";
    std::vector<std::string> arry;
    assert(helper.exec(select_sql, select_stu_callback, &arry));
    for(auto& name:arry){
        std::cout << name << std::endl;
    }
    //4.关闭数据库
    helper.close();

    return 0;
}